1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.truth.Truth.assertThat;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.annotations.GwtIncompatible;
23  import com.google.common.collect.testing.AnEnum;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.MapTestSuiteBuilder;
26  import com.google.common.collect.testing.TestEnumMapGenerator;
27  import com.google.common.collect.testing.features.CollectionFeature;
28  import com.google.common.collect.testing.features.CollectionSize;
29  
30  import junit.framework.Test;
31  import junit.framework.TestCase;
32  import junit.framework.TestSuite;
33  
34  import java.util.Map;
35  import java.util.Map.Entry;
36  
37  /**
38   * Tests for {@code ImmutableEnumMap}.
39   *
40   * @author Louis Wasserman
41   */
42  @GwtCompatible(emulated = true)
43  public class ImmutableEnumMapTest extends TestCase {
44    public static class ImmutableEnumMapGenerator extends TestEnumMapGenerator {
45      @Override
46      protected Map<AnEnum, String> create(Entry<AnEnum, String>[] entries) {
47        Map<AnEnum, String> map = Maps.newHashMap();
48        for (Entry<AnEnum, String> entry : entries) {
49          map.put(entry.getKey(), entry.getValue());
50        }
51        return Maps.immutableEnumMap(map);
52      }
53    }
54  
55    @GwtIncompatible("suite")
56    public static Test suite() {
57      TestSuite suite = new TestSuite();
58      suite.addTest(MapTestSuiteBuilder.using(new ImmutableEnumMapGenerator())
59        .named("Maps.immutableEnumMap")
60        .withFeatures(CollectionSize.ANY,
61            CollectionFeature.SERIALIZABLE,
62            CollectionFeature.ALLOWS_NULL_QUERIES)
63        .createTestSuite());
64      suite.addTestSuite(ImmutableEnumMapTest.class);
65      return suite;
66    }
67  
68    public void testEmptyImmutableEnumMap() {
69      ImmutableMap<AnEnum, String> map = Maps.immutableEnumMap(ImmutableMap.<AnEnum, String>of());
70      assertEquals(ImmutableMap.of(), map);
71    }
72  
73    public void testImmutableEnumMapOrdering() {
74      ImmutableMap<AnEnum, String> map = Maps.immutableEnumMap(
75          ImmutableMap.of(AnEnum.C, "c", AnEnum.A, "a", AnEnum.E, "e"));
76  
77      assertThat(map.entrySet()).has().exactly(
78          Helpers.mapEntry(AnEnum.A, "a"),
79          Helpers.mapEntry(AnEnum.C, "c"),
80          Helpers.mapEntry(AnEnum.E, "e")).inOrder();
81    }
82  }